<template>
  <div class="tabs">
    <el-tag v-for="(item, index) in tagsList" :key="item.name" :closable="item.name !== 'home'" :effect="$route.name === item.name ? 'dark' : 'plain'" @click="changeMenu(item)" @close="handleClose(item, index)">
      <i :class="'el-icon-' + item.icon"></i>
      {{ item.label }}
    </el-tag>
  </div>
</template>

<script>
export default {
  data() {
    return {}
  },

  methods: {
    // 点击跳转路由
    changeMenu(item) {
      console.log(this.$route)
      // item.checked = true
      this.$router.push({ name: item.name })
    },
    //删除tag标签
    handleClose(item, index) {
      const tags = this.$store.state.tabs.tabsList
      const length = tags.length - 1
      this.$store.commit('closeTab', item)

      //删除的不是高亮的标签
      if (item.name !== this.$route.name) {
        return
      }
      //删除的是高亮标签,且高亮标签右边没有其他标签，高亮向左移动
      if (index === length) {
        this.$router.push({ name: tags[index - 1].name })
      } else {
        //删除的是高亮标签，且高亮标签右边没有可删除的标签，高亮向右移动
        this.$router.push({ name: tags[index].name })
      }
    },
  },
  computed: {
    tagsList() {
      return this.$store.state.tabs.tabsList
    },
  },
}
</script>
<style>
.el-tag--plain .el-tag__close {
  color: #4060c7;
}
</style>
<style lang="less" scoped>
.tabs {
  height: 30px;
  margin: 12px 20px 10px 20px;
  background-color: #fff;

  .el-tag {
    color: #4060c7;
    margin-right: 15px;
    cursor: pointer;
  }
  .el-tag--dark {
    color: #fff;
    background-color: #4060c7;
    border-color: #4060c7;
  }

  .el-tag--plain {
    color: #4060c7;
    border-color: #607627;
  }
}
</style>
